<%@page language="java"  import="com.scalar.chiptrack.utils.ConfigManager" %>
<%@ page import="com.scalar.chiptrack.commons.Constants" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/purchaseOrderReceipt.do"/>

<%
    String chipTracContext = "/" + ConfigManager.getChipTracContext();
%>

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script language="javascript">
var noOfLots = 0;
function addPurchaseOrderLineItemLotDataEntry( )
{
    document.purchaseOrderReceiptForm.action='<%=chipTracContext%>/purchaseOrderReceipt.do?action=purchaseOrderLotEntryAddMore&openReceivingLineitemLotEntry=true';
    document.purchaseOrderReceiptForm.submit();
}

function SaveAndClose()
{
    var rowCnt = 0;
    for ( var i = 0; i < noOfLots; i++ )
    {
        var qtyIn = parseInt ( document.purchaseOrderReceiptForm['openPOLotList[' + i + '].qtyPulled'].value);
		var lotNumber = document.purchaseOrderReceiptForm['openPOLotList[' + i + '].lotNumber'].value;

		if( isNaN( qtyIn ) )
        {
            qtyIn = 0;
        }
        if ( lotNumber == '' && qtyIn == 0 )
        {
            rowCnt ++;
            continue;
        }

		if ( document.purchaseOrderReceiptForm['openPOLotList[' + i + '].selected'].checked  )
		{
			tmpErrMsg = validate( lotNumber, qtyIn, 0/*qtyOut*/ );
			if( tmpErrMsg != '' )
			{
				errorIndex.innerHTML = tmpErrMsg + " for row "+eval(i+1);
			    errorIndex.focus();
				return;
			}
		}
    }
    if( rowCnt == i )
    {
        errorIndex.innerHTML = "<li>Atleast one row should contain valid data";
        errorIndex.focus();
        return;
    }
    /*for ( var i = 0; i < noOfLots; i++ )
    {
        if ( document.purchaseOrderReceiptForm['openPOLotList[' + i + '].selected'].checked == false  )
        {
            document.purchaseOrderReceiptForm['openPOLotList[' + i + '].selected'].value = false;
            document.purchaseOrderReceiptForm['openPOLotList[' + i + '].selected'].checked = true;
        }
    }*/
    document.purchaseOrderReceiptForm.action='<%=chipTracContext%>/purchaseOrderReceipt.do?action=purchaseOrderLineItemLotEntryClose&openRecevingLineItemLotEntry=true';
    document.purchaseOrderReceiptForm.submit();
}

function validate( lotNumber, qtyIn, qtyOut )
{
    if( lotNumber != '' )
    {
        if ( qtyIn == 0 )
        {
          return "<li>Enter valid Quantity-In"
        }
        /*if ( qtyIn < qtyOut )
        {
            return "<li>Quantity-In cannot be less than Quantity-Out"
        }*/
    }
    else if( qtyIn != 0 )
    {
        if( lotNumber == '' )
        {
            return "<li>Lot Number cannot be blank if Quantity-In is entered"
        }
       /* if ( qtyIn < qtyOut )
        {
            return "<li>Quantity-In cannot be less than Quantity-Out"
        }*/
    }
   /* else if( qtyOut != 0 )
    {
        return "<li>Please enter Lot Number and Quantity-In"
    }*/
    return "";
}
function loadLotPartNumbers( index )
{
    document.purchaseOrderReceiptForm.action='<%=chipTracContext%>/purchaseOrderReceipt.do?action=<%=Constants.ACTION_PURCHASE_ORDER_LOAD_PART_NUMBERS%>&recevingLotIndex=' + index;
    document.purchaseOrderReceiptForm.submit();
}

function setSelected ( idx, chkBoxCtrl )
{
    var form = document.purchaseOrderReceiptForm;
    if ( chkBoxCtrl.checked )
    {
        form['openPOLotList['+idx+'].selected'].value = true;
		form['openPOLotList['+idx+'].qtyPulled'].value = form['openPOLotList['+idx+'].qtyIn'].value;
    }
    else
    {
        form['openPOLotList['+idx+'].qtyPulled'].value = 0;
    }
}
</script>
</HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
	    <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
        <TD bgcolor="#ffffff">
            <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
        <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
        <TD valign="top" width="100%">
            <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
            <BR>
<!-- from here your body starts -->
<html:form name="purchaseOrderReceiptForm"  type="com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm" action="/purchaseOrderReceipt.do">

             <table border="0" align = "left" width ="100%" >
             <TR>
                    <TD WIDTH="100%">
                        <TABLE BORDER="0" WIDTH="100%" HEIGHT="175%">
                            <TR>
                                <TD WIDTH="100%" align = "">
                                    <table width="75%" ALIGN="CENTER"><tr><td><center><FONT id="poErrors" styleClass="NArialL"><b>Purchase Order Line Item Lot Data Entry</b></FONT></center></td></TR></table>
                                </TD>
                            </TR>
                        </TABLE>
                        <BR>
                        <TABLE BORDER="0" ALIGN="CENTER" WIDTH="75%" CLASS="NArialL">
                            <tr>
                                <td colspan='3' class='NArialL' style='color:red' id="errorIndex"></td>
                            </tr>
                            <tr>
                                <td colspan='3'></td>
                            </tr>
                            <TR>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Select</FONT>
                                </TD>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Lot Number&nbsp;*</FONT>
                                </TD>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Quantity Available&nbsp;</FONT>
                                </TD>
								<TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Quantity Invoiced&nbsp;*</FONT>
                                </TD>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">Lot Status</FONT>
                                </TD>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">Process Name</FONT>
                                </TD>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">Part Number</FONT>
                                </TD>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">PO Number</FONT>
                                </TD>
                            </TR>
                            <%
                                boolean disable = false;
                            %>
                            <nested:size id="lotListSize" property="openPOLotList"/>
                            <script type="text/javascript">
                                noOfLots = <%=lotListSize.intValue()%>;
                            </script>
                            <nested:iterate property="openPOLotList" indexId="idx" id="purchaseOrderLineItemLotDataEntry" type="com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry">
                                   <%
                                        disable = false;
                                    %>
                                    <nested:greaterThan value="0" property="lotId">
                                    <%
                                            disable = true;
                                    %>
                                    </nested:greaterThan>
                                    <TR>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P> <nested:hidden property="selected"/>
                                                <input type="checkbox" class="txfield" onclick="setSelected(<%=idx.intValue()%>, this)" <%=purchaseOrderLineItemLotDataEntry.isSelected() ? "checked" : ""%> >                                                
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <input type="hidden" name="lineItemLot">
                                            <P><nested:text property="lotNumber" styleClass="txfield" size="19" maxlength="50" disabled="<%=disable%>" />
                                            </CENTER>
                                        </TD>
										<TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="qtyIn" styleClass="valuetxfield" size="19" maxlength="6" onkeypress="javascript:allowInteger()"  disabled="<%=disable%>"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="qtyPulled" styleClass="valuetxfield" size="19" onkeypress="javascript:allowInteger()" maxlength="6"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:write property="status"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                        <nested:greaterThan value="0" property="lotId">
                                            <nested:write property="psTypeName"/>
                                        </nested:greaterThan>
                                        <%
                                            String onChange = "loadLotPartNumbers('" + idx.intValue() + "')";
                                        %>
                                        <nested:lessEqual value="0" property="lotId">
                                            <nested:select property="psTypeName" styleClass="txfield" onchange="<%=onChange%>">
                                                <html:option value="" >----- Select -----</html:option>
                                                <html:options collection="processNameList" property="value" labelProperty="label"/>
                                            </nested:select>
                                        </nested:lessEqual>
                                        </TD>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                        <nested:greaterThan value="0" property="lotId">
                                            <nested:write property="partNumber"/>
                                        </nested:greaterThan>
                                        <nested:lessEqual value="0" property="lotId">
                                            <nested:select property="processStepId" styleClass="txfield">
                                                <html:option value="" >----- Select -----</html:option>
                                                <nested:present property="partNumbersList">
                                                    <nested:optionsCollection property="partNumbersList" label="label" value="value" />
                                                </nested:present>
                                            </nested:select>
                                        </nested:lessEqual>
                                        </TD>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:write property="poNumber"/>
                                            </CENTER>
                                        </TD>
                                    </TR>
                            </nested:iterate>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR align = "center" border="0">
                                <TD  bgColor= "" align="right" colspan = '6'>
                                <% String jsMethod = "javascript:addPurchaseOrderLineItemLotDataEntry()" ; %>
                                <A href="<%=jsMethod%>"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" onmouseover="window.status='Click to Create PO';return true" onmouseout="window.status='';return true" title="Click here to Create PO"></A>&nbsp;&nbsp;&nbsp;

                                </TD>
                            </TR>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR align = "center" border="0">
                                <TD  bgColor= "" align="center" colspan = '3'>
                                <A href="javascript:SaveAndClose()" ><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onmouseover="window.status='Click to Create PO';return true" onmouseout="window.status='';return true" title="Click here to Create PO"></A>&nbsp;&nbsp;&nbsp;
                                <A href="javascript:window.close()" onMouseOver="window.status='Click to Reset the PO Information';return true" onMouseOut="window.status='';return true" title="Click here to Reset Process Information"><IMG border=0 src="<%=chipTracContext%>/jsp/includes/images/cancel.gif"></a>
                                </TD>
                            </TR>
                        </TABLE>
                        <BR>
              </TABLE>
</html:form>
        <BR><BR><BR>
        </TD>
        <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
    </TR>
</TABLE>
</BODY>
</HTML>